require(RISmed)

pubmed.query.journal <- "\"%s\"[jo]"
pubmed.query.authors <- "%s[au]"
pubmed.query.date.interval <- "(\"%s\"[Date - Create] : \"%s\"[Date - Create])"

gs.query.authors <- "author:%s"

gs.get.all.by.author <- function(author, from=1950, to=2050, journal=""){
          
          author.split <- strsplit(author, " ")
          author.merge <- paste(sapply(author.split, FUN=function(x){sprintf(gs.query.authors, x)}), collapse=" ")
          
          input <- author.merge
          df <- GScholar_Scraper(input, since = from, to=to, citation = 1, journal="")
          return(df)
}

gs.get.all.by.journal <- function(journal, from=1950, to=2050){
          
         journal <- transformJournalForGS(journal) 
          
          df <- GScholar_Scraper(input = "", since = from, to=to, citation = 1, journal=journal)
          return(df)
}


pubmed.get.Journals.by.Author <- function(author){
          query <- sprintf(pubmed.query.authors, author)
          res <- EUtilsSummary(query)
          fetch <- EUtilsGet(res)
          return(unique(Title(fetch)))
}


pubmed.get.Journals.by.Author.Date <- function(author, from, to){
          query <- paste(
                    sprintf(pubmed.query.authors, author),
                    sprintf(pubmed.query.date.interval, from, to)
          )
          
          res <- EUtilsSummary(query)
          fetch <- EUtilsGet(res)
          return(unique(Title(fetch)))
}

pubmed.get.df.by.Author <- function(author, from=1950, to=2015){
          query <- paste(
                    sprintf(pubmed.query.authors, author),
                    sprintf(pubmed.query.date.interval, from, to)
          )
          
          res <- EUtilsSummary(query)
          fetch <- EUtilsGet(res)
          
          d <- buildDataFrame(fetch)
          
          return(d)
}

pubmed.get.df.by.Journal <- function(journal, from=1950, to=2015){
          
          journal <- transformJournalForPubmed(journal)
          query <- paste(
                    sprintf(pubmed.query.journal, journal),
                    sprintf(pubmed.query.date.interval, from, to)
          )
          
          res <- EUtilsSummary(query)
          fetch <- EUtilsGet(res)
          
          d <- buildDataFrame(fetch)
          
          return(d)
}


buildDataFrame <- function(fetch){
          journals <- Title(fetch)
          years <- Year(fetch)
          title <- ArticleTitle(fetch)
          pid <- PMID(fetch)
          type <- gsub(x=PublicationType(fetch), pattern=" ", replacement=".") 
          
          d <- data.frame(journals,years,title,pid,type)
          return(d)
}

i <- 1

pubgs <- function(data.pubmed, data.gs){
          
          pub.merge.gs <- c()
          for(i in 1:nrow(data.pubmed)){
                    pubmed.title <- paste(data.pubmed$title[i])
                    sims <- levenshteinSim(pubmed.title, c(paste(data.gs$TITLES)))
                    topsims <- which(sims > 0.60)
                    
                    if(length(topsims) == 0){
                              maxsim <- order(sims, decreasing=TRUE)[1]
                              cat(sprintf("\n\nfailed to find similar item.\n Pubmed:%s \n GS-Topsim:%s (%s)", pubmed.title, data.gs$TITLES[maxsim],sims[maxsim]))
                              next
                    }
                    
                    if(length(topsims > 1)){
                              cat(sprintf("pre:%s", topsims, "\n"))
                              maxtopsim <- 0
                              maxCIT <- 0
                              for(topsim in topsims){
                                        if(data.gs$CIT[topsim] > maxCIT){
                                                  maxtopsim <- topsim
                                                  maxCIT <- data.gs$CIT[topsim] 
                                        }
                              }
                              if(maxCIT == 0){
                                        maxtopsim <- topsims[1]
                              }
                              topsims <- maxtopsim
                              cat(sprintf("post:%s", topsims, "\n"))                   
                        }
                    
                    topgs <- data.gs[topsims,]
          
                    row <- c(paste(data.pubmed[i,"journals"]),
                                 paste(data.pubmed[i,"years"]),
                                 paste(data.pubmed[i,"title"]),
                                 paste(data.pubmed[i,"pid"]),
                                 paste(data.pubmed[i,"type"]),
                                 paste(topgs$TITLES),
                                 as.integer(topgs$CIT))
                    
                    pub.merge.gs <- rbind(pub.merge.gs, row)
          }
          
          pub.merge.gs <- data.frame(pub.merge.gs, row.names=NULL)
          colnames(pub.merge.gs) <- c("journal", "year","title","pid","type", "title_gs", "citation_gs")
          return(pub.merge.gs)
}

